import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getProductCategorySimpleList } from '#/api/erp/product/category';
import { getProductUnitSimpleList } from '#/api/erp/product/unit';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('views.erp.product.product.name'),
      rules: 'required',
      componentProps: {
        placeholder: $t('views.erp.product.product.namePlaceholder'),
      },
    },
    {
      fieldName: 'barCode',
      label: $t('views.erp.product.product.barCode'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.erp.product.product.barCodePlaceholder'),
      },
    },
    {
      fieldName: 'categoryId',
      label: $t('views.erp.product.product.category'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getProductCategorySimpleList();
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: $t('views.erp.product.product.categoryPlaceholder'),
        treeDefaultExpandAll: true,
      },
      rules: 'required',
    },
    {
      fieldName: 'unitId',
      label: $t('views.erp.product.product.unit'),
      component: 'ApiSelect',
      componentProps: {
        api: getProductUnitSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: $t('views.erp.product.product.unitPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'standard',
      label: $t('views.erp.product.product.standard'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.product.product.standardPlaceholder'),
      },
    },
    {
      fieldName: 'expiryDay',
      label: $t('views.erp.product.product.expiryDay'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.product.product.expiryDayPlaceholder'),
      },
    },
    {
      fieldName: 'weight',
      label: $t('views.erp.product.product.weight'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.product.product.weightPlaceholder'),
      },
    },
    {
      fieldName: 'purchasePrice',
      label: $t('views.erp.product.product.purchasePrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.product.product.purchasePricePlaceholder'),
        precision: 2,
        min: 0,
        step: 0.01,
      },
    },
    {
      fieldName: 'salePrice',
      label: $t('views.erp.product.product.salePrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.product.product.salePricePlaceholder'),
        precision: 2,
        min: 0,
        step: 0.01,
      },
    },
    {
      fieldName: 'minPrice',
      label: $t('views.erp.product.product.minPrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.product.product.minPricePlaceholder'),
        precision: 2,
        min: 0,
        step: 0.01,
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.erp.product.product.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.product.product.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'categoryId',
      label: $t('views.erp.product.product.category'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getProductCategorySimpleList();
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: $t('views.erp.product.product.categoryPlaceholder'),
        treeDefaultExpandAll: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'barCode',
      title: $t('views.erp.product.product.barCode'),
      minWidth: 120,
    },
    {
      field: 'name',
      title: $t('views.erp.product.product.name'),
      minWidth: 200,
    },
    {
      field: 'standard',
      title: $t('views.erp.product.product.standard'),
      minWidth: 100,
    },
    {
      field: 'categoryName',
      title: $t('views.erp.product.product.category'),
      minWidth: 120,
    },
    {
      field: 'unitName',
      title: $t('views.erp.product.product.unit'),
      minWidth: 100,
    },
    {
      field: 'purchasePrice',
      title: $t('views.erp.product.product.purchasePrice'),
      minWidth: 100,
      formatter: 'formatAmount2',
    },
    {
      field: 'salePrice',
      title: $t('views.erp.product.product.salePrice'),
      minWidth: 100,
      formatter: 'formatAmount2',
    },
    {
      field: 'minPrice',
      title: $t('views.erp.product.product.minPrice'),
      minWidth: 100,
      formatter: 'formatAmount2',
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
